// pages/my/my_favorite/my_favorite.js
const appInstance = getApp();
var url = appInstance.globalData.url;
var token = wx.getStorageSync('to_ken')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collectList: [],
    editAble: true,
    quanxuan: '全选',
    list: [],
    shoucang:false,
    editors_box:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.collectList();
  },

  //收藏列表
  collectList(e) {
    // console.log(e)
    var that = this;
    wx.request({
      url: url + '/follow/list',
      method: "POST",
      data: {
        pp_token: token,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        // console.log(res.data.data)
        that.setData({
          collectList: res.data.data
        })
      }
    })
  },
  //进入详情
  videtDetail(e) {
    // console.log(e.currentTarget.id)
    var id = e.currentTarget.id
    wx.navigateTo({
      url: '../../index/classify/videoDetail/videoDetail?id=' + id,
    })
  },
  //checkbox选中事件
  onChange(event) {
    console.log(event.currentTarget.id)
    var id = event.currentTarget.id;
    var list = this.data.collectList
    console.log(list)
    list.forEach(item => {
      console.log(item)
      if (item.video_id == id) {
        item.checked = !item.checked
      }
    })
    this.setData({
      collectList: list
    });
    //过滤
    this.filter();
  },
  //取消
  quxiaoTap() {
    var that = this;
    that.setData({
      editAble: true,
    })
    return false;
  },
  //编辑
  editors() {
    var that = this;
    that.setData({
      editAble: false,
    })
  },
  //全选
  quanxuanTap() {
    var that = this;
    if (this.data.quanxuan == '全选') {
      var newList = this.data.collectList;
      newList.forEach(item => {
        item.checked = true
      })
      that.setData({
        quanxuan: '取消全选',
        collectList: newList
      })
      //调过滤方法
      that.filter();
    } else if (this.data.quanxuan == '取消全选') {
      var newList = this.data.collectList;
      newList.forEach(item => {
        item.checked = false
      })
      that.setData({
        quanxuan: '全选',
        collectList: newList
      })
      //调过滤方法
      that.filter();
    }
  },

  //删除
  delTap(e) {
    var list = this.data.list;
    //通过map获取每个视频id
    var delList = this.data.list.map(item => {
      // console.log(item)
      return item.video_id
    })
    //拼接字符串
    var id = delList.join(',');
    console.log(id)
    console.log(delList)
    var that = this;
    wx.request({
      url: url + '/follow/delete',
      method: "POST",
      data: {
        pp_token: token,
        video_ids: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log(res)
      
          that.setData({
            collectList: res.data.data,
            shoucang:false,
            editAble:true
          })
      
        if (getCurrentPages().length != 0) {
          //刷新当前页面的数据
          getCurrentPages()[getCurrentPages().length - 1].onLoad()
        }
       
      }
    })
  },

  //过滤方法
  filter() {
    var that = this;
    var list = this.data.collectList.filter(item => {
      return item.checked
    })
    that.setData({
      list: list
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})